/*******************************************************************************
    Copyright 2019-2020 Sony Corporation

    $Id: //depot/bg/apps/audio/customer/Sony/LDAC-23.1-Earbud-Headset-QCC517x-QCC518x/audio/custom/kalimba/kymera/capabilities/ldac_decode/bufcnt/ldac_buffer_control.h#2 $
*******************************************************************************/
#ifndef LDAC_BUFFER_CONTROL_H__
#define LDAC_BUFFER_CONTROL_H__

#include <string.h>
#include <stdlib.h>
#include "capabilities.h"



#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#define NANOS 1000000000LL
#define MICROS 1000000L

#define BC_NUM_FRAME_OBSERVE                256
#define BC_NUM_FRAME_THRESHOULD_UPPER       6

#define BC_NUM_FRAME_THRESHOULD_LOWER       -20

#define DECODER_NUM_FRAMES_STARTS_DECODE    64

#define MAX_NUM_OF_COPY                     3
#define CONTINUOUS_DISCARD

typedef uint32 timespec;//1microsecond
/*
    enum buffer_control_state {
    BC_STATE_IDLE,
    BC_STATE_ANALYZING,
    BC_STATE_SKIP,
    BC_STATE_COPY,
    BC_STATE_BURST_ANALYZING,
    BC_STATE_BURST_SKIP,
    BC_STATE_BURST_RESUME,
};
*/
#define BC_STATE_IDLE               0
#define BC_STATE_ANALYZING          1
#define BC_STATE_SKIP               2
#define BC_STATE_COPY               3
#define BC_STATE_BURST_ANALYZING    4
#define BC_STATE_BURST_SKIP         5
#define BC_STATE_BURST_RESUME       6
    
struct buffer_control {
    uint32 frames_target;
    uint32 frames_upper;
    uint32 frames_lower;
    uint32 frame_status;
    uint32 index;
    uint32 skip_num;
    uint32 skip_flg;
    int32 num_of_copy;
    int32 max_num_of_copy;

    int32 frames[BC_NUM_FRAME_OBSERVE];
    int32 *pframes; /* current position */

    uint32 sum;    /* the sum of frames[] (to calculate SMA) */
    int32 control; /* flag to do control or not */
/*
    enum buffer_control_state stat;
*/
    int stat;
    timespec time;

    int32 burst_life; /* micro second */
    timespec burst_end;
    uint32 skip_cnt;
    uint32 skip_itr_index;
    uint32 skip_itr_sts;
    uint32 skip_itr_error;
    uint32 copy_cnt;
    uint32 copy_itr_error;
};
struct buffer_control *bc_new(uint32 num_of_frames);
/**
 * @param force reset burst mode if set to 1, or keep burst.
 */
void bc_reset(struct buffer_control *bc, bool force_reset);
void bc_update(struct buffer_control *bc, int32 frame, timespec now_msk);
void bc_burst(struct buffer_control *bc, int32 life, timespec now);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* LDAC_BUFFER_CONTROL_H__ */
